package com.cn.yz.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cn.yz.springboot.cache.RedisService;
import com.cn.yz.springboot.model.SysUser;
import com.cn.yz.springboot.service.SysUserService;

@RestController  
@EnableAutoConfiguration
public class MainController {

	private final static Logger logger = LoggerFactory.getLogger(MainController.class);

	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private RedisService redisService;

	@Autowired
	private RedisTemplate redisTemplate;

	@RequestMapping("/")
	public String hello(){
		logger.info("logback 成功了");
		logger.error("logback 成功了");
		return "Hello World";
	}
	
	@RequestMapping("/hello/{myName}")  
    String index(@PathVariable String myName) {  
        return "Hello "+myName+"!!!";  
    }  
	
	/**
	 * 测试springboot+mybaits
	 * @return
	 */
	@GetMapping("/list/all")
    public Object listAll() {
        return sysUserService.selectByPrimaryKey(1);
    }
	
	
	@RequestMapping("/setVal")
    public String setRedis() {
        // 保存字符串
        SysUser user = sysUserService.selectByPrimaryKey(26);
        redisService.put("1111", user);
        redisTemplate.opsForValue().set("name", "123");
        return "设置成功";
    }
	
	@RequestMapping("/getVal")
    public Object getRedis() {
        // 保存字符串
		return redisTemplate.opsForValue().get(26);
    }
}
